package sofia.bean;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.event.ValueChangeEvent;

import org.richfaces.model.CalendarDataModel;
import org.richfaces.model.CalendarDataModelItem;
import sofia.Persistencia.Entidades.ETerapia;
import sofia.Persistencia.Managers.MTerapia;
/**
 * ***************************************************************************<br/>
 * Establece algunas caracteristica para los calendarios y sus interacciones <br/>
 * ***************************************************************************
 * @author Jefferson Amado Peña
 */
public class CalendarDataModelImpl implements CalendarDataModel {

    private CalendarDataModelItem[] items;
    private String currentDescription;
    private String currentShortDescription;
    private Date currentDate;
    private boolean currentDisabled;


    /* (non-Javadoc)
     * @see org.richfaces.model.CalendarDataModel#getData(java.util.Date[])
     */
    /**
     *
     * @param dateArray
     * @return item de calendar
     */
    public CalendarDataModelItem[] getData(Date[] dateArray) {
        if (dateArray == null) {
            return null;
        }
        if (items==null) {
            items = new CalendarDataModelItem[dateArray.length];
            for (int i = 0; i < dateArray.length; i++) {
                items[i] = createDataModelItem(dateArray[i]);
            }
        }
        return items;
    }
    /**
     *
     * @param fecha
     * @return una pareja de datos
     */
    protected Map obtenerDatosSegunDia(Calendar fecha){
        List citas =  new MTerapia().obtenerTerapiasRegistradas();
           Calendar c = Calendar.getInstance();
        Map data=new HashMap();
            for(int i=0;i<citas.size();i++){
                    ETerapia et=(ETerapia)citas.get(i);
                    c.setTime(et.getFechaTerapia());
                    if(c.get(Calendar.DAY_OF_MONTH)==fecha.get(Calendar.DAY_OF_MONTH)&& c.get(Calendar.MONTH)== fecha.get(Calendar.MONTH)){
                        data.put("Cita","hora: "+et.getHoraTerapia()+" con:"+et.getFisioId());
                        //data.put("Descripcion",et.getComentarioVideo());
                    }
            }
        return data;
    }
    /**
     * @param date
     * @return CalendarDataModelItem for date
     */
    protected CalendarDataModelItem createDataModelItem(Date date) {
        CalendarDataModelItemImpl item = new CalendarDataModelItemImpl();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Map data = obtenerDatosSegunDia(c);
            item.setDay(c.get(Calendar.DAY_OF_MONTH));
                    //System.out.println("Dia: "+c.get(Calendar.DAY_OF_MONTH)+" Mes: "+c.get(Calendar.MONTH) );
            item.setEnabled(true);
            item.setStyleClass("rel-hol");
            item.setData(data);
        return item;
    }

    /* (non-Javadoc)
     * @see org.richfaces.model.CalendarDataModel#getToolTip(java.util.Date)
     */
    /**
     *
     * @param date
     * @return objeto con texto de ayuda
     */
    public Object getToolTip(Date date) {

        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @return items
     */
    public CalendarDataModelItem[] getItems() {
        return items;
    }

    /**
     * @param items
     */
    public void setItems(CalendarDataModelItem[] items) {
        this.items = items;
    }

    /**
     * Eventod e cambio de fecha en la interfaz
     * @param event 
     */
    public void valueChanged(ValueChangeEvent event) {
        System.out.println(event.getNewValue()+"selected");
        setCurrentDate((Date)event.getNewValue());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getCurrentDate());
        setCurrentDescription((String)((HashMap)items[calendar.get(Calendar.DAY_OF_MONTH)-1].getData()).get("description"));
        setCurrentShortDescription((String)((HashMap)items[calendar.get(Calendar.DAY_OF_MONTH)-1].getData()).get("shortDescription"));
    }

    /**
     * Storing changes action
     */
    public void storeDayDetails() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getCurrentDate());
        ((HashMap)items[calendar.get(Calendar.DAY_OF_MONTH)-1].getData()).put("shortDescription", getCurrentShortDescription());
        ((HashMap)items[calendar.get(Calendar.DAY_OF_MONTH)-1].getData()).put("description", getCurrentDescription());
    }

    /**
     * @return currentDescription
     */
    public String getCurrentDescription() {
        return currentDescription;
    }

    /**
     * @param currentDescription
     */
    public void setCurrentDescription(String currentDescription) {
        this.currentDescription = currentDescription;
    }

    /**
     * @return currentDisabled
     */
    public boolean isCurrentDisabled() {
        return currentDisabled;
    }

    /**
     * @param currentDisabled
     */
    public void setCurrentDisabled(boolean currentDisabled) {
        this.currentDisabled = currentDisabled;
    }

    /**
     * @return currentShortDescription
     */
    public String getCurrentShortDescription() {
        return currentShortDescription;
    }

    /**
     * @param currentShortDescription
     */
    public void setCurrentShortDescription(String currentShortDescription) {
        this.currentShortDescription = currentShortDescription;
    }

    /**
     * @return currentDate
     */
    public Date getCurrentDate() {
        return currentDate;
    }

    /**
     * @param currentDate
     */
    public void setCurrentDate(Date currentDate) {
        this.currentDate = currentDate;
    }

}